/**
 * 
 */
package com.wzl.ma;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

/**
 * @author Li Xiaohui
 * 
 */
public class TakePhoto extends Activity implements Callback {

	private Camera camera;
	private boolean isPreviewRunning = false;
	private SimpleDateFormat timeStampFormat = new SimpleDateFormat(
			"yyyyMMddHHmmssSS");

	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Uri target = Media.EXTERNAL_CONTENT_URI;
	private int PHOTO_WIDTH=480;
	private int PHOTO_HEIGHT=320;
	String currentPhotoFile = null;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Log.e(getClass().getSimpleName(), "onCreate");
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.pic);
		surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);
		// surfaceView = (ImageView) findViewById(R.id.ImageView01);
		surfaceHolder = surfaceView.getHolder();

		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuItem item = menu.add(0, 0, 0, "goto gallery");
		item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(Intent.ACTION_VIEW, target);
				startActivity(intent);
				return true;
			}
		});
		return true;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	Camera.PictureCallback mPictureCallbackJpeg = new Camera.PictureCallback() {
		public void onPictureTaken(byte[] data, Camera c) {
			Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
			// save the data to sdcard here
			// go to food editing page instead of showing preview.
			String filename = "pic" + timeStampFormat.format(new Date());
			String baseDir = "/sdcard/photos/";
			currentPhotoFile = baseDir + filename + ".jpg";

			try {
				FileOutputStream file = new FileOutputStream(new File(
						currentPhotoFile));
				file.write(data);
				file.flush();
				file.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
		public void onShutter() {
			Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
			// Set params in shuterCallback
			Camera.Parameters params = camera.getParameters();
			params.setPictureFormat(PixelFormat.JPEG);
			params.setPreviewSize(PHOTO_WIDTH, PHOTO_HEIGHT);
			camera.setParameters(params);

		}
	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		ImageCaptureCallback iccb = null;
//		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//			try {
//
//				ContentValues values = new ContentValues();
//				values.put(Media.TITLE, "test.jpg");
//				values.put(Media.DESCRIPTION, "Image capture by camera");
//				Uri photoPath = Uri.fromFile(new File("/sdcard/photos/"));
//				Uri uri = getContentResolver().insert(
//						photoPath, values);
//				// String filename = timeStampFormat.format(new Date());
//				iccb = new ImageCaptureCallback(getContentResolver()
//						.openOutputStream(uri));
//			} catch (Exception ex) {
//				ex.printStackTrace();
//				Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
//			}
//		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return super.onKeyDown(keyCode, event);
		}

		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			camera.takePicture(mShutterCallback, null, mPictureCallbackJpeg);
			return true;
		}

		return false;
	}

	protected void onResume() {
		Log.e(getClass().getSimpleName(), "onResume");
		super.onResume();
	}

	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	protected void onStop() {
		Log.e(getClass().getSimpleName(), "onStop");
		super.onStop();
	}

	public void surfaceCreated(SurfaceHolder holder) {
		Log.e(getClass().getSimpleName(), "surfaceCreated");
		camera = Camera.open();
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		Log.e(getClass().getSimpleName(), "surfaceChanged");
		if (isPreviewRunning) {
			camera.stopPreview();
		}
		// Camera.Parameters parameters = camera.getParameters();
		// parameters.setPictureFormat(PixelFormat.JPEG);
		// parameters.setPreviewSize(w, h);
		// camera.setParameters(parameters);

		try {
			camera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		camera.startPreview();
		isPreviewRunning = true;
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.e(getClass().getSimpleName(), "surfaceDestroyed");
		camera.stopPreview();
		isPreviewRunning = false;
		camera.release();
	}
}
